package com.yf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.dto.Page;
import com.yf.entity.Employee;
import com.yf.mapper.EmployeeMapper;
import com.yf.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void addEmployee(Employee employee) {
        employeeMapper.addEmployee(employee);
    }

    @Override
    public void deleteEmployee(Integer empId) {
        employeeMapper.deleteEmployee(empId);
    }

    @Override
    public void updateEmployee(Employee employee) {
        employeeMapper.updateEmployee(employee);
    }

    @Override
    public Employee getEmployeeByEmployeeId(Integer empId) {
        return employeeMapper.getEmployeeByEmployeeId(empId);
    }

    @Override
    public Page getEmployeePage(Integer pageNo, Integer pageSize) {
        Page page = new Page();

        PageHelper.startPage(pageNo,pageSize);
        List<Employee> employeeList = employeeMapper.getEmployeeList();
        PageInfo<Employee> info = new PageInfo<>(employeeList);

        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setPageCount(info.getPages());
        page.setCount((int) info.getTotal());
        page.setHasNext(info.isHasNextPage());
        page.setHasPre(info.isHasPreviousPage());
        page.setData(info.getList());

        return page;
    }

    /**
     * 通过员工名获取信息
     * @param empName
     * @return
     */
    @Override
    public Employee getEmployeeByName(String empName) {
        return employeeMapper.getEmployeeByName(empName);
    }
}
